<?php

	// TITLE: Organization AUTO MODEL
	// FILE: scaffolding/model/organization.php
	// AUTHOR: AUTOGEN
	// CREATED: 2015/04/20 07:49:57
	// DESCRIPTION: 
	// REQUIREMENTS: 

	$createOrganization = function($con, $organization) {
		$sql = 'INSERT INTO organization (id, type_id, name, description, address_id) VALUES (';
		$sql .= $organization->getId() . ', ';
		$sql .= $organization->getTypeId() . ', ';
		$sql .= $organization->getName() . ', ';
		$sql .= $organization->getDescription() . ', ';
		$sql .= $organization->getAddressId() . ')';
		$sql .= ')';
		$result = mysqli_query($con, $sql);
		$id = ($result) ? mysqli_insert_id($con) : $result;
		mysqli_close($con);
		return $id;
	};

	$readOrganization = function($con, $id) {
		$sql = 'SELECT * FROM organization WHERE id = ' . $id;
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$result = mysqli_fetch_array($results);
			$organization = new Organization();
			$organization->setId(result[0]);
			$organization->setTypeId($dbio->select($readType(result[1])));
			$organization->setName(result[2]);
			$organization->setDescription(result[3]);
			$organization->setAddressId($dbio->select($readAddress(result[4])));
		} else {
			$organization = false;
		}
		return $organization;
	};

	$updateOrganization = function($con, $organization) {
		$sql = 'UPDATE organization SET ';
		$sql .= 'id = ' . $organization->getId() . ', ';
		$sql .= 'type_id = ' . $organization->getTypeId() . ', ';
		$sql .= 'name = ' . $organization->getName() . ', ';
		$sql .= 'description = ' . $organization->getDescription() . ', ';
		$sql .= 'address_id = ' . $organization->getAddressId() ;
		$sql .= 'WHERE id = ' . id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$deleteOrganization = function($con, $id) {
		$sql = 'DELETE FROM organization WHERE id = ' . $id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$listOrganization = function($con) {
		$sql = 'SELECT * FROM organization';
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$organizations = array();
			while ($result = mysqli_fetch_array($results)) {
				$organization = new Organization();
				$organization->setId(result[0]);
				$organization->setTypeId($dbio->select($readType(result[1])));
				$organization->setName(result[2]);
				$organization->setDescription(result[3]);
				$organization->setAddressId($dbio->select($readAddress(result[4])));
				$organizations[] = organization;
			}// end while
		} else {
			$organizations = false;
		}
		return $organizations;
	};

